package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.dao.BaseDao;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.CancelOut;
import cn.com.ege.mvc.model.business.CancelShoe;
import cn.com.ege.mvc.model.business.OrderShoe;
import cn.com.ege.mvc.model.business.Shoe;
import cn.com.ege.mvc.model.business.ShoeWarehouse;
import cn.com.ege.mvc.service.impl.business.CancelOutService;
import cn.com.ege.mvc.service.impl.business.CancelShoeService;
import cn.com.ege.mvc.service.impl.business.OrderShoeService;
import cn.com.ege.mvc.service.impl.business.ShoeService;
import cn.com.ege.mvc.service.impl.business.ShoeWarehouseService;
import cn.com.ege.mvc.util.PageController;

public class CancelOutAction extends BaseAction implements IAction,
		Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private CancelOut cancelOut = null;
	private CancelOutService cancelOutService = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		
		String order_shoe_id = request.getParameter("order_shoe_id");//订单鞋款的id
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				
				int totalRows = cancelOutService
						.getRecordCount(cancelOut);
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<CancelOut> resourceList = cancelOutService.list(
						new CancelOut(), this.pager.getPageStartRow(),
						pager.getPageSize(), "", true);
				
				List<String> product_id_list = new ArrayList<String>();// 在页面中显示相应的成品料号
				OrderShoe orderShoe = new OrderShoe();
				OrderShoeService orderShoeService = new OrderShoeService();
				Shoe shoe = new Shoe();
				ShoeService shoeService = new ShoeService();
				for(CancelOut cancelOut:resourceList){
					orderShoe.setId(cancelOut.getOrder_shoe_id());
					shoe.setId(orderShoeService.get(orderShoe).getShoe_id());
					product_id_list.add(shoeService.get(shoe).getProduct_id());
				}
				request.setAttribute("product_id_list", product_id_list);
				this.pager.setData(resourceList);
				
				return getReturnURL(LIST);
			}
			// 点了添加 add
			else if ("add".equals(act)) {
					
				OrderShoe orderShoe = new OrderShoe();
				OrderShoeService orderShoeService = new OrderShoeService();
				orderShoe.setId(Long.parseLong(order_shoe_id));
				orderShoe = orderShoeService.get(orderShoe);
				
				//以下获取仓库内已有的该成品料号半成品鞋子的数量
				 Long[] owned = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
				ShoeWarehouse shoeWarehouse = new ShoeWarehouse();
				ShoeWarehouseService shoeWarehouseService = new ShoeWarehouseService();
				shoeWarehouse.setShoe_id(orderShoe.getShoe_id());
				shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
			    if(null != shoeWarehouse){
					owned[0] = shoeWarehouse.getSize_33();
					owned[1] = shoeWarehouse.getSize_34();
					owned[2] = shoeWarehouse.getSize_35();
					owned[3] = shoeWarehouse.getSize_36();
					owned[4] = shoeWarehouse.getSize_37();
					owned[5] = shoeWarehouse.getSize_38();
					owned[6] = shoeWarehouse.getSize_39();
					owned[7] = shoeWarehouse.getSize_40();
			    }
				request.setAttribute("order_shoe_id", order_shoe_id);
				request.setAttribute("shoe_id", orderShoe.getShoe_id());	
			    request.setAttribute("owned", owned);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {	
						ShoeWarehouse shoeWarehouse = new ShoeWarehouse();
						ShoeWarehouseService shoeWarehouseService = new ShoeWarehouseService();
						shoeWarehouse.setShoe_id(cancelOut.getShoe_id());
						shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
						// 修改冲单中该鞋款的剩余库存
						shoeWarehouse.setSize_33(shoeWarehouse.getSize_33() - cancelOut.getSize_33());
						shoeWarehouse.setSize_34(shoeWarehouse.getSize_34() - cancelOut.getSize_34());
						shoeWarehouse.setSize_35(shoeWarehouse.getSize_35() - cancelOut.getSize_35());
						shoeWarehouse.setSize_36(shoeWarehouse.getSize_36() - cancelOut.getSize_36());
						shoeWarehouse.setSize_37(shoeWarehouse.getSize_37() - cancelOut.getSize_37());
						shoeWarehouse.setSize_38(shoeWarehouse.getSize_38() - cancelOut.getSize_38());
						shoeWarehouse.setSize_39(shoeWarehouse.getSize_39() - cancelOut.getSize_39());
						shoeWarehouse.setSize_40(shoeWarehouse.getSize_40() - cancelOut.getSize_40());
						shoeWarehouseService.save(shoeWarehouse);
						cancelOutService.save(cancelOut);
						return getReturnURL("getList");
				} catch (BusinessException e) {
				
					request.setAttribute("cancelShoe", cancelOut);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					CancelShoe cancelShoe = new CancelShoe();
					//OrderShoe orderShoe = new OrderShoe();
					if (null != id && !("".equals(id))) {
						cancelShoe.setId(Long.parseLong(id));
						cancelOutService.delete(cancelOut);
					}
				}
				return getReturnURL("getList");
			}
		}
		addMessage("参数不正确!! act=" + act);
		return getReturnURL(ERROR);
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public CancelOut getCancelOut() {
		return cancelOut;
	}

	public void setCancelOut(CancelOut cancelOut) {
		this.cancelOut = cancelOut;
	}

	public CancelOutService getCancelOutService() {
		return cancelOutService;
	}

	public void setCancelOutService(CancelOutService cancelOutService) {
		this.cancelOutService = cancelOutService;
	}


}
